﻿using System.Linq;
using System.Web.Mvc;
using DataAccess.Entities;
using Zeenck.DataAccess.Repositories;

namespace Zeenck.Controllers
{
    [Authorize(Roles = "Admin")]
    public class InterestsController : Controller
    {
        IRepository<Interest> repo = new InterestRepository();

        public ViewResult Index()
        {
            return View(repo.GetAll().ToList());
        }

        public ViewResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Interest interest)
        {
            if (ModelState.IsValid)
            {
                repo.Save(interest);
                return RedirectToAction("Index");
            }

            return View(interest);
        }

        public ActionResult Edit(int id = 0)
        {
            Interest interest = repo.GetById(id);

            if (interest == null)
            {
                return HttpNotFound();
            }

            return View(interest);
        }

        [HttpPost]
        public ActionResult Edit(Interest interest)
        {
            if (ModelState.IsValid)
            {
                repo.Update(interest);
                return RedirectToAction("Index");
            }
            return View(interest);
        }

        public ActionResult Delete(int id)
        {
            Interest interest = repo.GetById(id);
            return View(interest);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Interest interest = repo.GetById(id);
            repo.Delete(id);
            return RedirectToAction("Index");
        }
    }
}
